package com.dxt.springmvcdemo.controller;

import com.dxt.springmvcdemo.exception.MyBusinessException;
import com.dxt.springmvcdemo.exception.MyBusinessExceptionEnum;
import com.dxt.springmvcdemo.pojo.Employee;
import com.dxt.springmvcdemo.service.EmployeeService;
import com.dxt.springmvcdemo.utils.R;
import com.dxt.springmvcdemo.vo.request.InsertEmployeeVo;
import com.dxt.springmvcdemo.vo.request.UpdateEmployeeVo;
import com.dxt.springmvcdemo.vo.response.SelectEmployeeVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;

/**
 * BelongProject: springMVC
 * BelongPackage: com.dxt.springmvcdemo.controller
 * Description:
 *
 * @Author 段鑫涛
 * @Create 2025/4/14 19:17
 * @Version 1.0
 */
@RestController
@Tag(name = "员工管理")
@Slf4j
@CrossOrigin
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/employee")
    @Operation(summary = "添加员工")
    public R insertEmployee(@RequestBody @Validated InsertEmployeeVo insertEmployeeVo){
        Employee employee = new Employee();
        BeanUtils.copyProperties(insertEmployeeVo,employee);
        employeeService.insertEmployee(employee);
        log.info("添加员工信息：{}",insertEmployeeVo);
        return R.success(200,"添加成功");
    }

    @DeleteMapping("/employee/{id}")
    @Operation(summary = "根据id删除员工")
    @Parameter(name = "id",description = "员工id",in = ParameterIn.PATH,required =true)
    public R deleteEmployeeById(@PathVariable Long id){
        try {
            this.getEmployeeById(id);
        } catch (Exception e) {
            throw new MyBusinessException(MyBusinessExceptionEnum.USER_NOT_EXIST);
        }
        employeeService.deleteEmployeeById(id);
        log.info("删除了id为{}的员工",id);
        return R.success(200,"删除成功");
    }

    @PutMapping("/employee")
    @Operation(summary = "更改员工信息")
    public R updateEmployee(@RequestBody @Validated UpdateEmployeeVo updateEmployeeVo){
        Employee employee = new Employee();
        BeanUtils.copyProperties(updateEmployeeVo,employee);
        employeeService.updateEmployee(employee);
        log.info("更新员工信息：{}",updateEmployeeVo);
        return R.success(200,"更新成功");
    }

    @GetMapping("/employee/{id}")
    @Operation(summary = "根据id查询员工")
    @Parameter(name = "id",description = "员工id",in = ParameterIn.PATH,required =true)
    public R<SelectEmployeeVo> getEmployeeById(@PathVariable Long id){
        Employee employee = employeeService.getEmployeeById(id);
        SelectEmployeeVo selectEmployeeVo = new SelectEmployeeVo();
        BeanUtils.copyProperties(employee,selectEmployeeVo);
        log.info("查询员工信息：{}",selectEmployeeVo);
        return R.success(200,"查询成功", selectEmployeeVo);
    }

    @GetMapping("/employees")
    @Operation(summary = "查询所有员工")
    public R<PageInfo<SelectEmployeeVo>> getEmployees(){
        PageHelper.startPage(2, 10);
        List<Employee> employeeList = employeeService.getEmployeeList();
        List<SelectEmployeeVo> selectEmployeeVoList = employeeList.stream().map(employee -> {
            SelectEmployeeVo selectEmployeeVo = new SelectEmployeeVo();
            BeanUtils.copyProperties(employee, selectEmployeeVo);
            return selectEmployeeVo;
        }).collect(Collectors.toList());
        var selectEmployeeVoPageInfo = new PageInfo<>(selectEmployeeVoList);
        log.info("查询所有员工信息：{}",selectEmployeeVoList);
        return R.success(200,"查询成功", selectEmployeeVoPageInfo);
    }


}
